<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */

	public function index()
	{
		// $this->ci_wechat->valid();

		$type = $this->ci_wechat->getRev()->getRevType();
		$this->MsgtypeHandle($type);
		// log_message('debug',$type);
		// switch($type) {
		// 	case CI_Wechat::MSGTYPE_TEXT:
		// 			$weObj->text("hello, I'm wechat")->reply();
		// 			exit;
		// 			break;
		// 	case CI_Wechat::MSGTYPE_EVENT:
		// 			break;
		// 	case CI_Wechat::MSGTYPE_IMAGE:
		// 			break;
		// 	default:
		// 			$weObj->text("help info")->reply();
		// }		

	}

	private function MsgtypeHandle($type)
	{
		# code...
		// log_message('debug',$type);
		switch ($type) {

			case CI_Wechat::MSGTYPE_TEXT:
				# code...
				$this->ci_wechat->text('<a href="https://www.baidu.com">baidu</a>')->reply();
				exit;
				break;
			case CI_Wechat::MSGTYPE_EVENT:
				$event_type = $this->ci_wechat->getRev()->getRevEvent();
				$this->MsgtypeEventHandle($event_type);
				break;
			default:
				# code...
				
				break;
		}
	}

	private function MsgtypeEventHandle($event_type) {
		switch ($event_type[CI_Wechat::MSGTYPE_EVENT]) {
			case CI_Wechat::EVENT_SUBSCRIBE:
				# code...
				$this->ci_wechat->text('关注了,<a href="https://www.baidu.com">baidu</a>')->reply();
				break;
			
			default:
				# code...
				$this->ci_wechat->text($event_type)->reply();
				break;
		}
	}

	public function getmenu()
	{
		# code...
		$menu = $this->ci_wechat->getMenu();
		$this->asJson($menu);
	}

	public function createmenu()
	{
		# code...
		$newmenu = array(
			'button' => array(
				array(
					'name' => '用户中心', 
					'sub_button' => array(
						array('name' => '手机绑定/解绑', 'type' => 'view', 'url' => 'https://www.baidu.com'),
					)
				),
			)
		);
		$result = $this->ci_wechat->createMenu($newmenu);
		$this->asJson($result);
	}

	private function asJson($data)
	{
		# code...
		$this->output->set_content_type('json', 'utf-8')->set_output(json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
	}

	public function getopenid()
	{
		# code...
		$json = $this->ci_wechat->getOauthAccessToken();
		if ($json) {
			$this->asJson($json);
			// echo json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
			return;
		}
		$callback = current_url();
		$state = 'wxbase';
		$scope = 'snsapi_base';
		$oauth_url = $this->ci_wechat->getOauthRedirect($callback,$state,$scope);
		redirect($oauth_url);
	}

	public function FunctionName($value='')
	{
		# code...
		$data['openid'] = $this->input->get('openid');
		$this->load->view('binding_phone_num', $data);
	}


	public function getopenid4ThirdParty() {
		$json = $this->ci_wechat->getOauthAccessToken();
		if ($json) {
			// echo $json['openid'];
			$redirect_uri1 = $this->input->get('redirect_uri1');
			$state = $this->input->get('state');
			if (strstr($redirect_uri1, "?")) {
				$redirect_uri1 = $redirect_uri1.'&openid='.$json['openid'].'$state='.$state;
			} else {
				$redirect_uri1 = $redirect_uri1.'?openid='.$json['openid'].'$state='.$state;
			}
			redirect($redirect_uri1);
			// $this->asJson($json);
			// echo json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
			return;
		}
		$callback = current_url();
		$redirect_uri1 = $this->input->get('redirect_uri');
		// $scope = $this->input->get('scope');
		$state = $this->input->get('state');

		if (!$redirect_uri1) {
			echo "缺少授权后重定向的回调链接地址";
			return;
		} 

		if (!$scope) {
			// snsapi_userinfo snsapi_base
			echo "缺少应用授权作用域";
			return;
		}



		if (!$state) {
			# code...
			$state = '';
		}

		if (strstr($callback, "?")) {
			$callback = $callback.'&redirect_uri1='.urlencode($redirect_uri1);
		} else {
			$callback = $callback.'?redirect_uri1='.urlencode($redirect_uri1);
		}

		$oauth_url = $this->ci_wechat->getOauthRedirect($callback, $state, $scope = 'snsapi_base');
		redirect($oauth_url);
	}

	public function test($value='')
	{
		# code...
		log_message('debug', 'request welcome test');
		echo "'debug', 'request welcome test'";
		exit();
	}
}
